pkg_name=gcc
major=4
minor=7
micro=4
version=$major.$minor.$micro
build_dir=$builds_dir_root/$pkg_name-cross-$version
src_dir=$src_dir_root/$pkg_name-$version
url=ftp://ftp.lip6.fr/pub/$pkg_name/releases/$pkg_name-$version/$pkg_name-$version.tar.bz2

rm -Rf "$src_dir"
cd "$src_dir_root"
tar jxf "$pkg_name-$version.tar.bz2"

#And the never ending gcc build bugs: now gcc is cross-compiled for a specific
#host, here a linux host... but one of the linux host source files rely on
#SSIZE_MAX macro defined only in posix1 enabled limits.h header file.
#Do patch it:
sed -i 's/SSIZE_MAX/((size_t)-1 >> 1)/' "$src_dir/gcc/config/host-linux.c"

rm -Rf "$build_dir"
mkdir -p "$build_dir"
cd "$build_dir"

export CFLAGS=-O0
"$src_dir/configure"					\
	"--build=$("$src_dir/config.guess")"		\
	--host=$gnu_target				\
	--target=$gnu_target				\
	--enable-serial-configure			\
	--prefix=/					\
	--enable-languages=c				\
	"--with-gmp=$sysroot"				\
	"--with-mpfr=$sysroot"				\
	"--with-mpc=$sysroot"				\
	"--with-build-time-tools=$cross_dir_root"       \
	--with-gnu-ld					\
	--with-gnu-as					\
	--disable-multilib				\
	--disable-nls
unset CFLAGS

make all-gcc
make all-target-libgcc

install_tmp_dir=/tmp/$pkg_name-cross-$version
rm -Rf "$install_tmp_dir"
mkdir -p "$install_tmp_dir"
make install-gcc "DESTDIR=$install_tmp_dir"
make install-target-libgcc "DESTDIR=$install_tmp_dir"

#fix installation paths on x86_64
mv "$install_tmp_dir/lib/gcc" "$install_tmp_dir/lib64"

install_dirs_in_sysroot "$builders_dir_root/$pkg_name/runtime-cross.dirs" "$sysroot/"
#we do not install the fixed include
install_files_in_sysroot "$builders_dir_root/$pkg_name/runtime-cross.files" "$install_tmp_dir/" "$sysroot/"
#we add the cc symbolic link to $gcc
install_links_in_sysroot "$builders_dir_root/$pkg_name/runtime-cross.links" "$sysroot/"
install_dirs_in_sysroot  "$builders_dir_root/$pkg_name/devel-cross.dirs" "$sysroot/"
install_files_in_sysroot "$builders_dir_root/$pkg_name/devel-cross.files" "$install_tmp_dir/" "$sysroot/"
install_links_in_sysroot "$builders_dir_root/$pkg_name/devel-cross.links" "$sysroot/"
elf_strip_in_sysroot "$builders_dir_root/$pkg_name/elf-strip-cross" "$sysroot/"

rm -Rf "$install_tmp_dir"
rm -Rf "$build_dir"
rm -Rf "$src_dir"
